'''
Created on 2010/07/06

@author: yukiyonezawa
'''

from pyrfid.DataProtocol.DSFID.AccessMethod.AccessMethod import AccessMethod
from pyrfid.DataProtocol.DSFID.DataFormat.DataFormat import DataFormat

from pyrfid.Util.Util import Util

class DSFID(object):
    '''
    Data Storage Object Identifier
    '''
    access_method = object()
    data_format = object() 
    
    def __init__(self, access_method_id, data_format_id):
        '''
        access_method -- a instance of Access Method(e.g NonDirectory)
        data_format -- a instance of Data Format(e.g NotFormatted) 
        '''
        print "----------------------------------------------"
        print "access method is ", access_method_id
        print "data format is ", data_format_id
        print "----------------------------------------------"
        
        
        self.access_method = AccessMethod.getAccessMethod(access_method_id)
        self.data_format = DataFormat.getDataFormat(data_format_id)
    
    
    @staticmethod
    def isDSFID(dsfid_on_hex):
        dsfid_on_bit = Util.convertHexToBit(dsfid_on_hex)
        
        #Data Format Extended
        if dsfid_on_bit[3:8] == "11111" and len(dsfid_on_hex) / 2 == 1 :
            return False
        
        return True
    
    @staticmethod
    def analyzeDSFID(dsfid_on_hex):
        """
        dsfid_on_hex -- string
        """
        # print dsfidOnHex
        dsfid_on_bit = Util.convertHexToBit(dsfid_on_hex)
        print dsfid_on_bit
        
        #access method dsfid_on_bit[8-7]
        #extend Indicator dsfid_on_bit[6]
        #data_format dsfid_on_bit[5-1]
        accessMethod = int(dsfid_on_bit[:2], 2)
        
        dataFormatOnBinary = dsfid_on_bit[2:]
        dataFormat = int()
        print dataFormatOnBinary
        if dataFormatOnBinary[0] == '1':
            raise "Not Implemented"
        
        dataFormatOnBinary = dataFormatOnBinary[1:]
        #Data Format Extended
        if dataFormatOnBinary[:5] == '11111':
            dataFormat = 32
            otherDataFormatOnBinary = dataFormatOnBinary[5:]
            for index in range(len(otherDataFormatOnBinary) / 8):
                dataFormat += int(otherDataFormatOnBinary[index*8:index*8+8], 2)
        
        else:
            dataFormat = int(dataFormatOnBinary[1:], 2)
                
        return DSFID(accessMethod, dataFormat)
        
    def getLogicalMemory(self):
        
        #bit 8 ~ 7
        #Add Access Method
        access_method_bit = Util.convertIntToBit(self.access_method.ACCESS_METHOD_ID, 2)
        # print dsfidBinary
        #Add 6 Extend Flog
        data_format_bit = '0'
        
        #Data Format
        if self.data_format.DATA_FORMAT_ID > 32: #extend
            data_format_bit += '11111'
            dsfidHex = self.encodeIntToHex(self.data_format.DATA_FORMAT_ID - 32 )
            data_format_bit += Util.convertHexToBit(dsfidHex)
            
        else:
            data_format_bit += Util.convertIntToBit(self.data_format.DATA_FORMAT_ID, 5)
        
        dsfid_on_logical_memory = Util.convertBitToHex( access_method_bit + data_format_bit)
        return dsfid_on_logical_memory
        
        